
// 列表中转tree
// 树里找目标id

const filterTree = (arr, id = null) => {
    // 第一次id未赋值，返回根节点
    // 查找parentId为此id的项，并进行递归调用
    return arr.filter(e => e.parentId === id)
      .map(e => {
        e.children = filterTree(arr, e.id)
        return e
      })
  }
  // 使用
  const test = [
    {
        "id": 1,
        "name": "bug菜单",
        "parentId": null,
        "type": 1
    },
    {
        "id": 2,
        "name": "404",
        "parentId": null,
        "type": 1
    },
    {
        "id": 3,
        "name": "123",
        "parentId": 1,
        "type": 1
    },
    {
        "id": 4,
        "name": "222",
        "parentId": 1,
        "type": 1
    },
    {
        "id": 7,
        "name": "333",
        "parentId": 3,
        "type": 1
    },
    {
        "id": 8,
        "name": "777",
        "parentId": 7,
        "type": 1
    },
    {
        "id": 10,
        "name": "888",
        "parentId": 8,
        "type": 1
    },
    {
        "id": 11,
        "name": "999",
        "parentId": null,
        "type": 1
    },
    {
        "id": 12,
        "name": "10",
        "parentId": 10,
        "type": 1
    }
  ]
  console.log(filterTree(test))
  
  
  
  function findSelectedIds(node) {
    let result = [];
    
    if (node.selected) {
      result.push(node.id);
    }
    
    if (node.children && node.children.length > 0) {
      for (let i = 0; i < node.children.length; i++) {
        const childIds = findSelectedIds(node.children[i]);
        result = result.concat(childIds);
      }
    }
    
    return result;
  }
  